package com.youlu.campus.service.ca.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.*;
import com.youlu.campus.entity.VO.req.*;
import com.youlu.campus.service.ca.CaTemplateService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;

@Slf4j
@Service
public class CaTemplateServiceImpl implements CaTemplateService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MapperFacade mapperFacade;


    @Override
    public void save(CreateCatemplateVO createCatemplate) {
        CaTemplate caTemplate = null;
        String caTemplateId = createCatemplate.getId();
        if(StringUtils.isEmpty(caTemplateId)){
            caTemplate = new CaTemplate();
            caTemplate.setCreatedTime(new Date());
        }else{
            caTemplate = mongoTemplate.findById(caTemplateId,CaTemplate.class);
            caTemplate.setUpdatedTime(new Date());
        }
         CaTemplateCertNo certificateNo = createCatemplate.getCertificateNo();
        CertificateNoInfo certificateNoInfo = new CertificateNoInfo();
        BeanUtils.copyProperties(certificateNo,certificateNoInfo);
        caTemplate.setCertificateNo(certificateNoInfo);
        CaTemplateUniversityVO universityVO = createCatemplate.getUniversity();
        if(Objects.nonNull(universityVO)){
            CaTemplateUniversity caTemplateUniversity = new CaTemplateUniversity();
            BeanUtils.copyProperties(universityVO,caTemplateUniversity);
            caTemplate.setUniversity(caTemplateUniversity);
        }else{
            caTemplate.setUniversity(null);
        }

        CaTemplateUserNameVO userNameVO = createCatemplate.getUserName();
        if(Objects.nonNull(userNameVO)){
            CaTemplateUserName userName = new  CaTemplateUserName ();
            BeanUtils.copyProperties(userNameVO,userName);
            caTemplate.setUserName(userName);
        }else{
            caTemplate.setUserName(null);
        }
        CaBackgroundPicVO backgroundPicVO = createCatemplate.getBackgroundPic();
        CaBackgroundPic caBackgroundPic = new CaBackgroundPic();
        BeanUtils.copyProperties(backgroundPicVO,caBackgroundPic);
        caTemplate.setBackgroundPic(caBackgroundPic);
        caTemplate.setCaName(createCatemplate.getCaName());
        caTemplate.setCaTime(createCatemplate.getCaTime());
        caTemplate.setStamp(createCatemplate.getStamp());
        caTemplate.setPaperCaTemplateInfo(createCatemplate.getPaperCaTemplateInfo());
        if(Objects.nonNull(createCatemplate.getCaTemplateUniversityUserName())){
            caTemplate.setCaTemplateUniversityUserName(createCatemplate.getCaTemplateUniversityUserName());
        }else{
            caTemplate.setCaTemplateUniversityUserName(null);
        }
        this.mongoTemplate.save(caTemplate);
    }

    @Override
    public Page<CaTemplate> list(QueryCatemplateVO queryCatemplate) {
        String templateName = queryCatemplate.getTemplateName();
        Query query = new Query();
        if(StringUtils.isNotBlank(templateName)){
            Pattern pattern = Pattern.compile("^.*" + templateName + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("caName").regex(pattern));
        }
        PageRequest pageRequest = PageRequest.of(queryCatemplate.getPage() - 1, queryCatemplate.getPageSize());
        long count = this.mongoTemplate.count(query, CaTemplate.class);
        if(count==0){
            return PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0);
        }
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<CaTemplate> list = this.mongoTemplate.find(query.with(pageRequest), CaTemplate.class);
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public CaTemplate detail(String catemplateId) {
        return this.mongoTemplate.findById(catemplateId,CaTemplate.class);
    }

    @Override
    public CaTemplate findByActivityId(String activityId,String caLevel) {
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("caLevel").is(caLevel));
        return this.mongoTemplate.findOne(query,CaTemplate.class);
    }

    @Override
    public List<CaTemplate> findByActivityId(String activityId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        return this.mongoTemplate.find(query,CaTemplate.class);
    }

    @Override
    public void caCaTemplateCopyById(String copyId) {
        CaTemplate caTemplate = this.detail(copyId);
        if(Objects.isNull(caTemplate)){
            return;
        }
        Date now = new Date();
        CaTemplate targetCaTemplate =mapperFacade.map(caTemplate,CaTemplate.class);
        targetCaTemplate.setId(null);
        targetCaTemplate.setCaName(caTemplate.getCaName()+"copy");
        targetCaTemplate.setCreatedTime(now);
        targetCaTemplate.setUpdatedTime(now);
        this.mongoTemplate.save(targetCaTemplate);
    }
}
